package com.smart.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.Department;
import com.smart.pojo.ResultBean;
import com.smart.req.DepartmentReq;
import com.smart.service.IDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("queryAll")
    public ResultBean queryAll() {
        return ResultBean.success(departmentService.list());
    }

    @GetMapping("queryPageByCondition")
    public ResultBean queryPageByCondition(DepartmentReq departmentReq) {
        System.out.println("查询部门的条件：" + departmentReq);
        Page<Department> departmentPage = new Page<>(departmentReq.getPageNum(), departmentReq.getPageSize());
        departmentService.lambdaQuery()
                .like(!ObjectUtils.isEmpty(departmentReq.getDepartmentName()), Department::getName, departmentReq.getDepartmentName())
                .page(departmentPage);
        return ResultBean.success(departmentPage);
    }

    @PostMapping("save")
    @RequiresPermissions({"dept:add"})
    public ResultBean save(@RequestBody Department department) {
        System.out.println("新增的部门信息：" + department);
        boolean rs = departmentService.addDept(department);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    @PutMapping("update")
    @RequiresPermissions({"dept:save"})
    public ResultBean update(@RequestBody Department department) {
        System.out.println("更新的部门信息：" + department);
        boolean rs = departmentService.updateDept(department);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    @GetMapping("getDepartmentById/{id}")
    public ResultBean getDepartmentById(@PathVariable("id") Long id) {
        System.out.println("查询的部门Id：" + id);
        return ResultBean.success(departmentService.getById(id));
    }
}
